package ls.test;

import ls.dao.CourseDAO;
import ls.dao.DAOFactory;
import ls.dao.EvaluateDAO;
import ls.dao.StudentDAO;
import ls.entity.Evaluate;
import ls.entity.Student;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class TeacherEvaluate {
    CourseDAO courseDAO = DAOFactory.getInstance().getCourseDAO();
    EvaluateDAO evaluateDAO = DAOFactory.getInstance().getEvaluateDAO();
    StudentDAO studentDAO = DAOFactory.getInstance().getStudentDAO();
    public int teacherEvaluate(String teacher_id)
    {
        List<Evaluate> evaluateList = new ArrayList<>();
        evaluateList = evaluateDAO.getTeacherEvaluate(teacher_id);
        if(evaluateList.size()==0)
        {
            System.out.println("您暂时没有需要处理的研究生助教评定表！");
            int m = middle(teacher_id);
            if(m == 0) return 0;
        }
        else if(evaluateList.size() == 1)
        {
            impl(teacher_id,evaluateList.get(0).getCourse_name(),evaluateList.get(0));
        }
        else
        {
            System.out.println("经检测您需要处理的评定表不止一个，对应的信息分别为：");
            evaluate(evaluateList);
            System.out.println("请选择您要处理的评定表对应的课程名");
            Scanner scanner = new Scanner(System.in);
            String courseName = scanner.next();
            boolean b = isName(evaluateList,courseName);
            if(b)
            {
                Evaluate evaluate = null;
                for (int i = 0;i < evaluateList.size();i++)
                {
                    if(courseName.equals(evaluateList.get(i).getCourse_name())) {
                        evaluate = evaluateList.get(i);
                        break;
                    }
                }
                impl(teacher_id,courseName,evaluate);
            }
            else {
                System.out.println("您输入的课程名不在上面的List中！");
                int m = middle(teacher_id);
                if(m == 0) return 0;
            }
        }
        return 0;
    }
    public int middle(String teacher_id)
    {
        System.out.println("请选择您要进行的操作：");
        System.out.println("1.课程信息录入");
        System.out.println("2.助教选择");
        System.out.println("3.审核助教工作评定表");
        System.out.println("4.查看已录入的我的全部课程");
        System.out.println("5.查看我已处理的全部评定表");
        System.out.println("6.退出系统");
        Scanner scanner = new Scanner(System.in);
        int choose = scanner.nextInt();
        if(choose == 1)
        {
            CreateCourse createCourse = new CreateCourse();
            createCourse.create(teacher_id);
        }
        else if(choose == 2)
        {
            ChooseStudent cs = new ChooseStudent();
            cs.chooseStudent(teacher_id);
        }
        else if(choose == 3)
        {
            TeacherEvaluate te = new TeacherEvaluate();
            te.teacherEvaluate(teacher_id);
        }
        else if(choose == 4)
        {
            ChooseStudent chooseStudent = new ChooseStudent();
            chooseStudent.showCourse(teacher_id);
        }
        else if(choose == 5)
        {
            getMyEvaluate(teacher_id);
        }
        else if(choose == 6)
        {
            return 0;
        }
        return 0;
    }

    public int impl(String teacher_id,String courseName,Evaluate evaluate)
    {
        System.out.println("该评定表内容为：");
        evaluate2(evaluate);
        Student student = new Student();
        student.setStudent_id(evaluate.getStudent_id());
        student.setStudent_name(evaluate.getStudent_name());
        student.setCourse(courseName);
        System.out.println("请输入您的审核结果：1.通过 2.不通过");
        Scanner scanner = new Scanner(System.in);
        int r = scanner.nextInt();
        String result = "";
        if(r == 1)
        {
            result = "通过";
            student.setState("已结束");
            evaluate.setResult(result);
            studentDAO.updateStudentState(student);
            evaluateDAO.updateEvaluate(evaluate);
            int m = middle(teacher_id);
            if(m == 0) return 0;
        }
        else if(r == 2)
        {
            result = "不通过";
            evaluate.setResult(result);
            student.setState("无助教");
            student.setFirstVol("null");
            student.setSecondVol("null");
            student.setCourse("null");
            studentDAO.updateDefeat(student);
            evaluateDAO.updateEvaluate(evaluate);
            int m = middle(teacher_id);
            if(m == 0) return 0;
        }
        else {
            System.out.println("输入不合法！");
            int m = middle(teacher_id);
            if(m == 0) return 0;
        }
        return 0;
    }
    public void evaluate(List<Evaluate> evaluateList)
    {
        for(int i = 0;i < evaluateList.size();i++)
        {
            System.out.print("课程名："+evaluateList.get(i).getCourse_name());
            System.out.print("   助教姓名："+evaluateList.get(i).getStudent_name());
            System.out.print("   助教学号："+evaluateList.get(i).getStudent_id());
            System.out.println();
        }
    }

    public boolean isName(List<Evaluate> evaluateList,String courseName)
    {
        int flag = 0;
        for(int i = 0;i < evaluateList.size();i++)
        {
            if(courseName.equals(evaluateList.get(i).getCourse_name()))
            {
                flag = 1;
                break;
            }
        }
        if(flag == 0)
            return false;
        else return true;
    }

    public void evaluate2(Evaluate evaluate)
    {
        System.out.print("研究生姓名："+evaluate.getStudent_name());
        System.out.print("    研究生学号："+evaluate.getStudent_id());
        System.out.println();
        System.out.print("课程名称："+evaluate.getCourse_name());
        System.out.print("    授课人数："+evaluate.getNumber());
        System.out.println();
        System.out.print("课程性质："+evaluate.getCourse_property());
        System.out.print("    授课对象："+evaluate.getCourse_student());
        System.out.println();
        System.out.print("授课教师："+evaluate.getCourse_teacher());
        System.out.print("    授课时间："+evaluate.getTeach_time());
        System.out.println();
        System.out.println("助教工作自述："+evaluate.getStudent_work());
        System.out.println("教师评价结果："+evaluate.getResult());
    }

    public int getMyEvaluate(String teacher_id)
    {

        List<Evaluate> evaluateList = evaluateDAO.getMyAllEvaluate(teacher_id);
        for (int i = 0;i < evaluateList.size();i++)
        {
            evaluate2(evaluateList.get(i));
            System.out.println();
        }
        int m = middle(teacher_id);
        return 0;
    }

}
